package com.nine.liteflow.component;

import com.nine.liteflow.context.CustomContext;
import com.nine.liteflow.data.DataRequest;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@LiteflowComponent("b")
public class BCmp extends NodeComponent {
    @Override
    public void process() throws Exception {

        CustomContext contextBean = this.getContextBean(CustomContext.class);
        // 或者你也可以用这个方法去获取上下文实例，如果你只有一个上下文，那么和上面是等价的
        // CustomContext context = this.getFirstContextBean();

        String cid = contextBean.getCid();
        String remark = contextBean.getRemark();
        log.info(">>>>>>>>>>组件b>>>>>cid:{}, remark:{}", cid, remark);

        log.info(">>>>>>>>>>组件b.process...");


        // 测试流程手动结束
        DataRequest requestData = this.getRequestData();
        Map<String, Object> params = requestData.getParams();
        Boolean bEnd = (Boolean) params.get("bEnd");
        log.info(">>>>>>>>>>isEnd:{}", isEnd());
        if (bEnd) {
            this.setIsEnd(true);
        }
        log.info(">>>>>>>>>>isEnd:{}", isEnd());

    }

    /**
     * 是否结束整个流程(不往下继续执行)。
     * 如果返回true，则表示在这个组件执行完之后立马终止整个流程。此时由于是用户主动结束的流程，属于正常结束，所以流程结果中(LiteflowResponse)的isSuccess是true。
     */
    @Override
    public boolean isEnd() {
        return super.isEnd();
    }

    @Override
    public void onSuccess() throws Exception {
        super.onSuccess();
        log.info(">>>>>>>>>>onsuccess");
    }
}
